﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.EnterpriseServices;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Data;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    #region 增加会员
    //[WebMethod]
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public string Customer_Add(string key, string sddm, string gkdm, string gkmc, string gksex, string pwd, string email,string address,string postCode,string mobile,string phone, string ip,string ReCommendCustomer,string Nike,string province,string city,string area)
    {
        if (sddm.Trim() == "" || gkdm.Trim() == "" || gkmc.Trim() == "" || gksex.Trim() == "")
        { 
            return "-2";
        }
        else
        {
            if (DB.CheckCode(key))
            {
                try
                {
                    string UserCode = DB.InsertVIP(sddm, gkdm, gkmc, gksex, pwd, email, address, postCode, mobile, phone, ReCommendCustomer, Nike, province, city, area);
                    ContextUtil.SetComplete();
                    return UserCode;
                }
                catch
                {
                    ContextUtil.SetAbort();
                    return "0";
                }
            }
            else
            {
                return "-1";
            }
        }
    }
    #endregion

    #region 更新会员信息
    [XmlInclude(typeof(Model.V_CUSTOMER))]
    [XmlInclude(typeof(Model.V_CUSTOMER_AUTO))]
    //[WebMethod]
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public string Customer_Update(string key,Model.V_CUSTOMER customer,Model.V_CUSTOMER_AUTO vc_auto)
    {
        string result = "";
        if (DB.CheckCode(key))
        {
            try
            {
                result = DB.Customer_Update(customer, vc_auto);
                if (result == "111")
                {
                    ContextUtil.SetComplete();                   
                }
                else
                {
                    ContextUtil.SetAbort();                    
                }
            }
            catch
            {
                result = "b";
                ContextUtil.SetAbort(); 
            }
        }
        else
        {
            result = "a";
        }
        return result;
    }
    #endregion

    #region 更新会员密码
    //[WebMethod]
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public string Customer_UpPwd(string key, string UserCode, string Npwd)
    {
        string result = "";
        if (DB.CheckCode(key))
        {
            try
            {
                result = DB.UpPwd(UserCode, Npwd);
                if (result == "11")
                {
                    ContextUtil.SetComplete();                   
                }
                else
                {
                    ContextUtil.SetAbort();                    
                }
            }
            catch
            {
                result = "b";
                ContextUtil.SetAbort(); 
            }
        }
        else
        {
            result = "a";
        }
        return result;
    }
    #endregion

    #region 更新信用额度、结余金额、积分

    #region 更新结余款和可用发货金额,当仅更新1条数库时调用此方法
    //[WebMethod]
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public bool UpdateBalanceAndUsableDeliverAmountOfOneRecord(string key, string[] strArrayToUpdateBStyle, string[] strToUpdateERP)
    {
        bool result = false;
        if (DB.CheckCode(key))
        {
            try
            {
                result = DB.UpdateBalanceAndUsableDeliverAmount(strArrayToUpdateBStyle, strToUpdateERP);
                if (result)
                {
                    ContextUtil.SetComplete();
                }
                else
                {
                    ContextUtil.SetAbort();
                }
            }
            catch
            {
                ContextUtil.SetAbort();
            }
        }
        return result;
    }

    #endregion
    #region 更新结余款和可用发货金额,当更新多于1条数库时调用此方法

    //[WebMethod]
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public void UpdateBalanceAndUsableDeliverAmountMoreThanOneRecord(string key, string[] strArrayToUpdateBStyle, string[] strToUpdateERP)
    {
        if (DB.CheckCode(key))
        {
            char[] charToTrim = new char[] { '|' };
            string[] sqlCommandArrayToUpdateBStyleDB;
            string[] sqlCommandArrayToUpdateErpDB;
            for (int i = 0; i < strArrayToUpdateBStyle.Length; i++)
            {
                sqlCommandArrayToUpdateBStyleDB = strArrayToUpdateBStyle[i].TrimEnd(charToTrim).Split(charToTrim);
                sqlCommandArrayToUpdateErpDB = strToUpdateERP[i].TrimEnd(charToTrim).Split(charToTrim);
                try
                {
                    if (DB.UpdateBalanceAndUsableDeliverAmount(sqlCommandArrayToUpdateBStyleDB, sqlCommandArrayToUpdateErpDB))
                    {
                        ContextUtil.SetComplete();
                    }
                    else
                    {
                        ContextUtil.SetAbort();
                    }
                }
                catch
                {
                    ContextUtil.SetAbort();
                }

            }
        }
    }
    #endregion
    #endregion



    #region 订单修改保存
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public int P_Order_Update(string key, string orderid, string strtmp, string strtmp2, Model.ORDERDOC od2)
    {
        Model.ORDERDOC od = new BLL.ORDERDOC().GetModel(orderid);
        od.ORDER_OFFERAMT = od2.ORDER_OFFERAMT;
        od.ORDER_TRANCOST = od2.ORDER_TRANCOST;
        od.ORDER_NINV = od2.ORDER_NINV;
        od.ORDER_NOTE = od2.ORDER_NOTE;
        od.ORDER_GETER = od2.ORDER_GETER;
        od.ORDER_GETERPHONE1 = od2.ORDER_GETERPHONE1;
        od.ORDER_GETERPHONE2 = od2.ORDER_GETERPHONE2;
        od.ORDER_GETPOSTCODE = od2.ORDER_GETPOSTCODE;
        od.ORDER_GETADDRESS = od2.ORDER_GETADDRESS;
        od.ORDER_TRANERCODE = od2.ORDER_TRANERCODE;
        od.ORDER_COSTNATURE = od2.ORDER_COSTNATURE;
        od.ORDER_DTCODE = od2.ORDER_DTCODE;
        od.ORDER_OFCODE = od2.ORDER_OFCODE;
        od.ORDER_WEIGHT = od2.ORDER_WEIGHT;
        od.ORDER_INVTITLE = od2.ORDER_INVTITLE;
        od.ORDER_PAYAMT = od2.ORDER_PAYAMT;

        int result = 0;
        if (DB.CheckCode(key))
        {
            try
            {
                if (!new BLL.ORDERDETAIL().Delete(orderid))
                {
                    result = -1;
                }
                if (!new BLL.PAYDETAIL().Delete(orderid))
                {
                    result = -2;
                }
                if (strtmp.Trim() != "")
                {
                    string[] tmp = strtmp.Split('#');
                    for (int a = 0; a < tmp.Length; a++)
                    {
                        string[] sptmp = tmp[a].Split(',');
                        if (!new BLL.ORDERDETAIL().P_Order_InsertProductMX(orderid, sptmp[0], sptmp[1], sptmp[2], Int32.Parse(sptmp[3].Trim()), Decimal.Parse(sptmp[4])))
                        {
                            result = -3;
                        }
                    }
                }
                if (strtmp2.Trim() != "")
                {
                    string[] tmp2 = strtmp2.Split('#');
                    for (int b = 0; b < tmp2.Length; b++)
                    {
                        string[] zftmp = tmp2[b].Split(',');
                        if (new BLL.PAYDETAIL().Add(orderid, zftmp[1], Decimal.Parse(zftmp[2]), zftmp[3], zftmp[4], DateTime.Parse(zftmp[5]), zftmp[0], zftmp[6]) == null)
                        {
                            result = -4;
                        }
                    }
                }
                if (!new BLL.ORDERDOC().Update(od))
                {
                    result = -5;
                }
                //else
                //{
                //    if (od.ORDER_DTCODE == "003")
                //    {
                //        Model.ORDERDOC od5 = new BLL.ORDERDOC().GetModel(od.ORDER_ID);
                //        if (od5.ORDER_AAMT != od.ORDER_AAMT)
                //        {
                //            V_VIPSET vp = new V_VIPSET();
                //            if (!new BLL.V_VIPSET().VipSet_Up_ByZD13(od.ORDER_CUSTOMERCODE, od.ORDER_AAMT - od5.ORDER_AAMT))
                //            {
                //                result = -9;
                //            }
                //        }
                //    }
                //}
            }
            catch
            {
                result = -999;
                ContextUtil.SetAbort();
            }
        }
        else
        {
            result = -998;
        }
        if (result == 0)
        {
            ContextUtil.SetComplete();
        }
        else
        {
            ContextUtil.SetAbort();
        }
        return result;
    }
    #endregion

    #region 财务审批订单
    //[XmlInclude(typeof(List<Model.PAYDOCPAYDETAIL>))]
    //[XmlInclude(typeof(Model.PAYDOC))]
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public int P_Order_CAudit(string key, string orderid, List<Model.PAYDOCPAYDETAIL> ppdlist, Model.PAYDOC pd, int orderacheck)
    {
        int result = 0;
        if (DB.CheckCode(key))
        {
            try
            {
            string v = new BLL.ORDERDOC().P_Order_CAudit(orderid, ppdlist, pd,orderacheck);
            if (v.Length > 0)
            {
                V_VIPSET vvp = new V_VIPSET();
                Model.V_VIPSET mvp = vvp.GetModel(v);

                DataSet ds_pd = new BLL.PAYDETAIL().P_Order_GetProductZFMX(orderid);
                decimal ljje = 0.00M;
                for(int a=0;a<ds_pd.Tables[0].Rows.Count;a++)
                {
                    Model.PAYDETAIL pd_1 = new BLL.PAYDETAIL().GetModel(orderid, ds_pd.Tables[0].Rows[a]["PD_DETAILID"].ToString().Trim());

                    switch(ds_pd.Tables[0].Rows[a]["PD_DTCODE"].ToString().Trim())
                    {
                        case "001":
                            if (orderacheck == 0)
                            {
                                pd_1.PD_GETAMT = 0.00M;
                                pd_1.PD_GET = 0;
                                ljje += -1*pd_1.PD_PAYAMT;
                                mvp.BYZD13 += -1*pd_1.PD_PAYAMT;
                            }
                            else
                            {
                                pd_1.PD_GETAMT = pd_1.PD_PAYAMT;
                                pd_1.PD_GET = 1;
                                ljje += pd_1.PD_PAYAMT;
                                mvp.BYZD13 += pd_1.PD_PAYAMT;
                            }
                            new BLL.PAYDETAIL().Update(pd_1);
                            
                            break;
                        //case "003":
                        //    mvp.BYZD13 += -1*pd_1.PD_PAYAMT;
                        //    break;
                    }



                }
                //mvp.DQJF += Convert.ToInt32(pd.PD_SUBAMT/mvp.JFJE); 
                
                if (vvp.Update(mvp))
                {
                    Model.V_VIP_ACCOUNT vva = new BLL.V_VIP_ACCOUNT().GetModel(v);
                    if (vva!=null)
                    {
                        vva.DQJE_1 += ljje;
                        if (new BLL.V_VIP_ACCOUNT().Update(vva))
                        {

                        }
                        else
                        {
                            result = -3;
                        }
                    }
                    else
                    {
                        Model.V_VIP_ACCOUNT vvaa = new Model.V_VIP_ACCOUNT();
                        vvaa.DM = v;
                        vvaa.DQJE_1 = ljje;
                        if (new BLL.V_VIP_ACCOUNT().Add(vvaa) != null)
                        {
                            
                        }
                        else
                        {
                            result = -3;
                        }
                    }
                    string[] a = new string[1];
                    a.SetValue(" if exists (select 1 from v_vip_account where dm='" + v + "' ) begin update v_vip_account set dqje_1=dqje_1+" + ljje + " where dm='" + v + "' end else begin insert into v_vip_account (dm,dqje_1) values ('" + v + "'," + ljje + ") end  ", 0);
                    if (DB.UpdateErpDB(a))
                    {
                        result = 1;
                    }
                    else
                    {
                        result = -4;
                    }
                }
                else
                {
                    result = -2;
                }
            }
            else
            {
                result = -1;
            }
            }
            catch
            {
                result = -999;
                ContextUtil.SetAbort(); 
            }
        }
        else
        {
            result = -998;
        }
        if (result >0)
        {
            ContextUtil.SetComplete();
        }
        else
        {
            ContextUtil.SetAbort();
        }
        return result;
    }
    #endregion

    #region 数据转结
    //[WebMethod]
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public int SaleStat(string key, DataSet ds)
    {
        DataTable dt = new DataTable();
        dt = ds.Tables[0];
        if (DB.CheckCode(key))
        {
            try
            {
                if (DB.Write1(dt))
                {
                    if (DB.Write2())
                    {
                        if (DB.Write3(dt))
                        {
                            ContextUtil.SetComplete();
                            return 1;
                        }
                        else
                        {
                            ContextUtil.SetAbort();
                            return -5;
                        }
                    }
                    else
                    {
                        ContextUtil.SetAbort();
                        return -4;
                    }
                }
                else
                {
                    ContextUtil.SetAbort();
                    return -3;
                }
            }
            catch
            {
                ContextUtil.SetAbort();
                return -2;
            }
        }
        else
        {
            return -1;
        }
    }
    #endregion

    #region 购物积分分成计算
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    //[WebMethod]
    public string inergalSend(string key,string customer, DateTime time)
    {
        string result = "";
        if (DB.CheckCode(key))
        {            
            DataSet ds = new BLL.Recofull_OrderBL().GetOrderForSendJF(customer, time);
            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    try
                    {
                        //系统积分参数设置
                        Model.IntergalTypeSet IntergalTypeSetModel = BLL.IntergalTypeSet.GetSysInterTypeSet();
                        if (IntergalTypeSetModel == null)
                        {
                            result = "积分/现金分摊数据未设置";
                        }
                        else
                        {
                            if (IntergalTypeSetModel.IntergalSetExtendAttribute6 < 1)
                            {
                                result = "未设置分摊级次";
                            }
                            else
                            {
                                //获取启用层数的积分分成比例
                                BLL.RecommendLevelSet RecommendLevelSetbll = new BLL.RecommendLevelSet();
                                List<Model.RecommendLevelSet> lists = null;
                                string where = "RecommendManLevel<=" + IntergalTypeSetModel.IntergalSetExtendAttribute6.ToString();
                                lists = RecommendLevelSetbll.GetList(null, where, null);
                                if (lists == null)
                                {
                                    result = "积分/现金分摊数据未设置";
                                }
                                else
                                {
                                    BLL.InergalManage bllmanage = new BLL.InergalManage();
                                    List<string> list = null;
                                    Model.SYSPARAM sysparam = new BLL.SYSPARAM().GetModel("007");
                                    foreach (DataRow dr in ds.Tables[0].Rows)
                                    {
                                        list = bllmanage.InergalShare(customer, decimal.Parse(dr["ZJF"].ToString()), dr["ORDER_ID"].ToString(), lists, IntergalTypeSetModel, sysparam,DateTime.Parse(dr["ORDER_CREATEDATE"].ToString()));
                                        if (DB.InsertIntergal(list))
                                        {
                                            DB.UpdateCustomerDqjf(dr["ORDER_ID"].ToString());
                                            DB.UpdateDDJFDivide(dr["ORDER_ID"].ToString());
                                            ContextUtil.SetComplete();                                            
                                        }
                                        else
                                        {
                                            ContextUtil.SetAbort();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                        ContextUtil.SetAbort();
                    }
                }
            }
            else
            {
                result = "无日结分摊数据";
            }
        }
        return result;
    }
    #endregion

    #region 不涉及分成的加积分统用方法
    /// <summary>
    /// 不涉及分成的加积分统用方法
    /// </summary>
    /// <param name="customer">用户ID</param>
    /// <param name="intergarl">积分</param>
    /// <param name="from">用户ID</param>
    /// <param name="intertype">积分类型</param>
    /// <param name="time">时间</param>
    /// <returns></returns>
    //[WebMethod]
    [WebMethod(TransactionOption = TransactionOption.RequiresNew)]
    public int AddOtherJF(string key,string customer, decimal intergarl,string from, BLL.InergalManage.InergalTypeEnum intertype,string time)
    {
          int result = 0;
          if (DB.CheckCode(key))
          {
              List<string> list = null;
              list = new BLL.InergalManage().InergalShare(customer, intergarl, from, intertype, time);
              List<string> list1 = new List<string>();
              list1.Add("UPDATE [V_VIPSET] SET [DQJF]=[DQJF]+" + intergarl + " WHERE [GKDM]='" + customer + "'");
              try
              {
                  if (list != null)
                  {
                      DB.InsertIntergal(list);
                      DB.UpdateCustomerDqjf(list1);
                  }
                  result = 1;
                  ContextUtil.SetComplete();
              }
              catch
              {
                  ContextUtil.SetAbort();
              }
          }
          return result;
    }
    #endregion

    #region 客服回复时，根据用户留言以及商品评价，对用户进行积分奖罚，更新用户当前积分
    /// <summary>
    /// 客服回复时，根据用户留言以及商品评价，对用户进行积分奖罚，更新用户当前积分
    /// </summary>
    /// <param name="key">网店代码</param>
    /// <param name="customer">用户ID</param>
    /// <param name="jf">积分</param>
    /// <param name="sql">其他sql操作语句</param>
    /// <returns>返回0表示操作失败，1表示成功</returns>
    //[WebMethod]
    [WebMethod(TransactionOption=TransactionOption.RequiresNew)]
    public int UpdateCustomerDQJF(string key, string customer, decimal jf, string sql)
    {
        int result = 0;
        if (DB.CheckCode(key))
        {
            List<string> list = new List<string>();
            if (sql != "")
            {
                DB.UpdateBStyleDB(new string[]{sql});
            }
            list.Add("UPDATE [V_VIPSET] SET [DQJF]=[DQJF]+" + jf + " WHERE [GKDM]='" + customer + "'");
            try
            {
                if (list != null)
                {
                    DB.UpdateCustomerDqjf(list);
                }
                result = 1;
                ContextUtil.SetComplete();
            }
            catch
            {
                ContextUtil.SetAbort();
            }
        }
        return result;

    }
    #endregion
}
